package com.nativetalk.controller.app.three.teacher.book;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.book.TdNtBookCourseUpdate;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.Constants;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.controller.app.two.setmeal.TwoSetmealController;
import com.nativetalk.service.three.book.search.ThreeBookSearchService;
import com.nativetalk.service.three.book.update.ThreeBookUpdateService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class ThreeTeacherBookController {
    private static Logger logger = Logger.getLogger(ThreeTeacherBookController.class);
    @Resource
    private ThreeBookSearchService threeBookSearchService;
    @Resource
    private ThreeBookUpdateService threeBookUpdateService;
    /**
     * @Purpose  查找可接单课程列表
     * @version  3.1
     * @time     2015-07-29
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value = "/app/7/teatoken/book/canConnect", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo canConnect(@RequestBody Map<String, Object> map,
                           @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
            map.put("tea_id",tdNtTeacherResult.getTea_id());
            map.put("pageSize",10);
            map.put("state",1);
            retInfo = threeBookSearchService.findBookByState(map);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @Purpose  查找可接单课程列表
     * @version  3.1
     * @time     2015-07-29
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value = "/app/7/teatoken/book/connect", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo connect(@RequestBody Map<String, Object> map,
                           @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
            map.put("tea_id",tdNtTeacherResult.getTea_id());
            map.put("pageSize",10);
            retInfo = threeBookSearchService.findBookByTeaId(map);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }
    /**
     * @Purpose  查找用户设计课程表可上课时间
     * @version  3.1
     * @time     2015-07-29
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value = "/app/7/teatoken/book/connect/week", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo week(@RequestBody Map<String, Integer> map,
                            @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            retInfo = threeBookSearchService.findBookWeek(map.get("book_cou_id"));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }
    /**
     * @Purpose  接单后预约信息
     * @version  3.1
     * @time     2015-07-29
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value = "/app/7/teatoken/book/connect/week/his", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo his(@RequestBody Map<String, Integer> map,
                       @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            retInfo = threeBookSearchService.findBookWeekHis(map.get("book_cou_id"));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }
    /**
     * @Purpose  接单
     * @version  3.1
     * @time     2015-07-29
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value = "/app/7/teatoken/book/orders", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo orders(@RequestBody TdNtBookCourseUpdate tdNtBookCourseUpdate,
                          @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtBookCourseUpdate));

            RetInfo retInfo;
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
            tdNtBookCourseUpdate.setTea_id(tdNtTeacherResult.getTea_id());
            retInfo = threeBookUpdateService.updateBook(tdNtBookCourseUpdate);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }

}


